package com.front.web.mall_part_front;

import com.bootdo.common.utils.CurrencyUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.mall.domain.MallOrder;
import com.bootdo.mall_part.domain.MallEvaluate;
import com.bootdo.member.domain.ShequMember;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/f/evaluateController")
public class MallEvaluateFrontController extends MallSuperFrontController{

    /**
     * 添加评论
     * http://localhost/f/evaluateController/addEvaluate
     * @param orderId
     * @param starNumber
     * @param content
     * @param mobile
     * @return
     */
    @RequestMapping("addEvaluate")
    public R addEvaluate(@RequestParam(value = "orderId")String orderId,
                         @RequestParam("starNumber")Integer starNumber,
                         @RequestParam(value = "content", defaultValue = "")String content,
                         @RequestParam("mobile")String mobile) {
        ShequMember member = shequMemberService.getByMobile(mobile);
        if (member == null) {
            return R.error("用户信息错误");
        }

        MallOrder mallOrder = mallOrderService.getByOrderId(orderId);
        if (mallOrder == null) {
            return R.error("订单不存在");
        }
        if (mallOrder.getPayStatus() != 3) {
            return R.error("该订单不允许进行评价");
        }

        if (mallOrder.getMemberMobile() != null && !Objects.equals(mallOrder.getMemberMobile(), mobile)) {
            return R.error("订单信息与用户信息不符");
        }

        if (mallEvaluateService.addEvaluates(member, mallOrder, content, starNumber) == 0) {
            return R.error("评价失败");
        }

        return R.ok("评价成功");
    }

    /**
     * 回复商家
     * http://localhost/f/evaluateController/replierMerchant
     * @param evaluateId
     * @param mobile
     * @param content
     * @return
     */
    @RequestMapping("replierMerchant")
    public R replierMerchant(@RequestParam(value = "evaluateId")Long evaluateId,
                            @RequestParam("mobile")String mobile,
                             @RequestParam("content")String content) {
        ShequMember member = shequMemberService.getByMobile(mobile);
        MallEvaluate mallEvaluate = mallEvaluateService.get(evaluateId);
        if (mallEvaluate == null) {
            return R.error("参数错误");
        }
        mallEvaluate.setId(null);
        mallEvaluate.setReplierId(mallEvaluate.getCommentId());
        mallEvaluate.setReplierType(2);
        mallEvaluate.setReplierName(mallEvaluate.getCommentName());
        mallEvaluate.setReplierAvater(mallEvaluate.getCommentAvater());

        mallEvaluate.setCommentId(member.getId());
        mallEvaluate.setCommentName(member.getRealName());
        mallEvaluate.setCommentType(1);
        mallEvaluate.setCommentAvater(member.getAvatar());

        mallEvaluate.setContent(content);
        mallEvaluate.setParentId(evaluateId);

        if (mallEvaluateService.save(mallEvaluate) == 0) {
            return R.error("评价失败");
        }
        return R.ok("评价成功").put("i", mallEvaluate);
    }

    /**
     * 得到商品的评价
     * http://localhost/f/evaluateController/getEvaluate
     * @param goodsId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("getEvaluate")
    public R getEvaluate(@RequestParam(value = "goodsId")Long goodsId,
                         @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
                         @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize) {

        // 得到商品的评价
        Map<String, Object> param = new HashMap<>();
        param.put("offset", pageNo * pageSize);
        param.put("limit", pageSize);
        param.put("goodsId", goodsId);
        List<MallEvaluate> list = mallEvaluateService.getByGoodsId(param);
        Integer total = mallEvaluateService.getByGoodsIdCount(param);
        param.put("starFlag", 1);
        Integer goodsTotal = mallEvaluateService.getByGoodsIdCount(param);
        double goodsRate=0;
        if(total!=0){
            goodsRate = CurrencyUtils.multiply(CurrencyUtils.divide(new BigDecimal(goodsTotal), new BigDecimal(total)), new BigDecimal(100)).doubleValue();
        }

        Map<String, Object> param2 = new HashMap<>();
        for (MallEvaluate one: list) {
            param2.put("grandFatherId", one.getId());
            param2.put("offset", 0);
            param.put("limit", 1);
            List<MallEvaluate> sonList = mallEvaluateService.getSonList(param2);
            one.setSonList(sonList);
            one.setSonTotal(sonList.size());
        }

        return R.ok().put("l", list).put("total", total).put("goodsRate", goodsRate);
    }

    /**
     * 得到更多的商品评论
     *  http://localhost/f/evaluateController/getMoreEvaluate
     * @param evaluateId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("getMoreEvaluate")
    public R getMoreEvaluate(@RequestParam(value = "evaluateId")Long evaluateId,
                             @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
                             @RequestParam(value = "pageSize", defaultValue = "3")Integer pageSize) {

        // 得到商品的评价
        Map<String, Object> param = new HashMap<>();

        param.put("offset", pageNo * pageSize);
        param.put("limit", pageSize);
        param.put("grandFatherId", evaluateId);

        List<MallEvaluate> sonList = mallEvaluateService.getSonList(param);
        return R.ok().put("l", sonList);
    }

    // 得到未读的条数，如果传pageSize和pageNo就会返回条数以及总的未读数组
    @RequestMapping("getMemberUnreadCount")
    public R getMemberUnreadCount(@RequestParam(value = "mobile")String mobile,
                                  @RequestParam Map<String, Object> param) {

        ShequMember member = shequMemberService.getByMobile(mobile);
        if (member == null) {
            return R.error("参数错误");
        }

        Map<String, Object> unreadEvaluate = mallEvaluateService.getMemberUnreadEvaluate(member, param);

        return R.ok(unreadEvaluate);
    }

    /**
     * 得到单个订单评价的对话详情
     * @param id
     * @param mobile
     * @return
     */
    @RequestMapping("getOneEvaluateDetail")
    public R getOneEvaluateDetail(@RequestParam(value = "id")Long id,
                                  @RequestParam("mobile")String mobile) {
        ShequMember member = shequMemberService.getByMobile(mobile);
        if (member == null) {
            return R.error("用户信息错误");
        }

        MallEvaluate evaluate = mallEvaluateService.get(id);

        if (evaluate.getMemberId() != member.getId().longValue()) {
            return R.error();
        }
        List<MallEvaluate> mallEvaluates = mallEvaluateService.getByFatherId(id);
        mallEvaluates.add(0, evaluate);
        MallEvaluate endEvaluate = mallEvaluates.get(mallEvaluates.size() - 1);
        mallEvaluateService.setReaded(id, endEvaluate.getId(), member.getId());

        return R.ok().put("l", mallEvaluates);
    }



}
